import Vue from 'vue'
import Vuex from 'vuex'
import { baseUrl, baseUrl2 } from '../helper/helper1.js'
import { ribaoUrl } from '../helper/ribao.js'
import ajax from '../helper/ajax.js'
import router from '../router'

Vue.use(Vuex)

const state = {
    tableData: [],
    ribaodata: [],
    banjiData: [],
    studentName:[]
}

const mutations ={
    changeData( state, parm){
        this.state.tableData = parm
        for(var i =0; i<state.banjiData.length;i++){
            for(var j =0; j<state.tableData.length;j++){
                if(state.banjiData[i].id == state.tableData[j].banji){
                    state.tableData[j].banji = state.banjiData[i].name
                }
            }
        }
    },

    RbData(state,param){
        this.state.ribaodata = param
    },
    getClassName(state,param){
        state.banjiData = param
    },
    getstName(state, param){
        state.studentName = param
    }
   
}
const actions ={
     getData({commit},ipt){
         let params = {
             transform: 1,
             order: 'id,desc',
             params: params
         }
        if(ipt ==''){
            return
         }
        if(ipt){
            params.filter = `name,eq,${ipt}`
        }
        ajax({
            method: 'get',
            url: baseUrl,
            params: params,
            success:( res ) =>{
                // console.log(res.xy_users)
                commit('changeData', res.xy_users)
            // console.log(this.state.tableData)
            }
        })
    },
    getStudentName(context, param) {
        ajax({
            url: baseUrl,
            method: 'get',
            params: {
                transform: 1,
                order: 'id,desc',
                filter: 'banji,eq,' + param
            },
            success: (data) => {
                console.log('data....',data)
                context.commit('getstName',data.xy_users)
            }
        })
    },
    getClass(context){
        ajax({
            url: baseUrl2,
            method: 'get',
            params:{
                transform: 1,
                order: 'id,desc'
            },
            success: (data) => {
                // console.log('data....',data)
                let className = data.xy_banji
                context.commit('getClassName',className)
               
            }
        })
    },
    addInfo( conext, p){
        router.push('./stF')    
    },

    getRibaoForm(conext,pra){
        router.push('./rbF')    
    },

    edit(conext, params){
        // console.log(params.stData)
        let router = params.router
        if (params.stData ){
            let aihaoArr = params.stData.aihao.split(',')// 做修改的时候需要把页面上的字符串的爱好转为数组
            params.stData.aihao = aihaoArr 
            router.push({ name: "studentForm", params: params.stData})
        }
        router.push('./stF')  
    },
    saveData(context,agmt){
        let student = agmt.student, router = agmt.router
        let method = 'post', url = baseUrl, stIde = student.id
        if(stIde){
            method:'put'
            url: `${url}/${stIde}`
        }

        if(student.aihao){
            let aihao = student.aihao.join(',')
            student.aihao = aihao
        }
            ajax({
                method: method,
                url: url,
                data: student ,
                success: (res) => {
                    // console.log(res)
                    router.push('./st')
                }
            })
    },
    saveRibaoMsg(context,param){
        param.name = param.banji + param.name
        // console.log(param)
        ajax({
            url: baseUrl2,
            method: 'post',
            data: param,
            success: (data) => {
                // console.log('data1....',data)
                context.dispatch('getRibaoData')
                router.push('./rb')
            }
        })
    },
    // getRibaoData({commit}){
    getRibaoData(context){
        ajax({
            url: ribaoUrl,
            method: 'get',
            params:{
                transform: 1,
                order: 'id,desc'
           },
            success: (data) => {
                // console.log('日报=', data)
                context.commit('RbData', data.xy_ribao)
                // console.log(context)
            }
        })
    }
}

export default new Vuex.Store({
    state,
    actions,
    mutations
})
   